package com.situ.day3;

import org.junit.Test;

public class ArrayDemo {
    @Test
    public void test1() {
        int score1 = 34;
        int score2 = 74;
        int score3 = 84;
        int sum = score1 + score2 + score3;

        int[] array = new int[4];
        //数组的访问：数组名[下标]
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        //属性length
        System.out.println("length: " + array.length);//4
        for (int i = 0; i < array.length; i++) {
            //System.out.println(i);
            System.out.println(array[i]);
        }
        System.out.println("------------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test55() {
        int[] array = new int[4];
        //数组的访问：数组名[下标]
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        int sum = 0;
        //就数组的最大值、最小值
        for (int i = 0; i < array.length; i++) {
            sum = sum + array[i];
        }
        System.out.println("sum: " + sum);
    }

    @Test
    public void test515() {
        int[] array = new int[4];
        //数组的访问：数组名[下标]
        array[0] = 23;
        array[1] = 34;
        array[2] = 4;
        array[3] = 64;
        //假设第一个是最大的
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test518() {
        int[] array = new int[4];
        //数组的访问：数组名[下标]
        array[0] = 23;
        array[1] = 34;
        array[2] = 4;
        array[3] = 64;

        int max = getMax(array);
        System.out.println("max: " + max);
    }

    /**
     * 传递过来任何一个数组，然后返回数组最大值
     * @param array
     * @return
     */
    public int getMax(int[] array) {
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test516() {
        int[] array = new int[4];
        //数组的访问：数组名[下标]
        array[0] = 23;
        array[1] = 34;
        array[2] = 4;
        array[3] = 64;
        //假设第一个是最小的
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("min: " + min);
    }

    @Test
    public void test56() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 34;
        array[2] = 4;
        array[3] = 64;
        //java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
        for (int i = 0; i <= array.length; i++) {
            System.out.println(array[i]);
        }
    }
    
    @Test
    public void test566() {
        int[] array = {40, 17, 21, 1};
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] < array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}
